package org.arclib.x.light;

import org.arclib.Texture;
import org.arclib.draw.Color;
import org.arclib.math.Point;
import org.lwjgl.opengl.GL11;

/// a light source
public class Light
{
	// /
	private Point position;
	// /
	private Color color = Color.White;

	// / the light does not provide any illumination further away than that
	private double outerradius = 128;

	/**
	 * Controls the size of the lightsource and thereby the softness of shadows.
	 * If lightblockers are smaller than this, there'll be artifacts.
	 **/
	double sourceradius = 5;

	public Light(Point argPoint, Color argColor, int argOuter, int argInner)
	{
		// TODO Auto-generated constructor stub
		setPosition(argPoint);
		setColor(argColor);
		setOuterRadius(argOuter);
		sourceradius = argInner;
	}

	public Light(Point argPoint, Color argColor, int argOuter)
	{
		// TODO Auto-generated constructor stub
		setPosition(argPoint);
		setColor(argColor);
		setOuterRadius(argOuter);
	}

	// /
	public void draw()
	{
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		Color.Yellow.setGLColor();

		GL11.glBegin(GL11.GL_TRIANGLE_FAN);

		GL11.glVertex2d(getPosition().x, getPosition().y);

		int segments = 20;
		for (int i = 0; i < segments + 1; ++i)
		{
			Point p = getPosition().add(
					Point.fromPolar(sourceradius, 2 * Math.PI * i / segments));
			GL11.glVertex2d(p.x, p.y);
		}

		GL11.glEnd();
	}

	// /
	public void draw(Point offset)
	{
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		Color.Yellow.setGLColor();

		GL11.glBegin(GL11.GL_TRIANGLE_FAN);

		Point p = offset.add(getPosition());
		GL11.glVertex2d(p.x, p.y);

		int segments = 20;
		for (int i = 0; i < segments + 1; ++i)
		{
			Point p2 = offset.add(getPosition().add(
					Point.fromPolar(sourceradius, 2 * Math.PI * i / segments)));
			GL11.glVertex2d(p2.x, p2.y);
		}

		GL11.glEnd();
	}

	public void setPosition(Point position)
	{
		this.position = position;
	}

	public Point getPosition()
	{
		return position;
	}

	public void setOuterRadius(double outerradius)
	{
		this.outerradius = outerradius;
	}

	public double getOuterRadius()
	{
		return outerradius;
	}

	public void setColor(Color color)
	{
		this.color = color;
	}

	public Color getColor()
	{
		return color;
	}

	public static Texture texture;
}
